var $ = exports

var StartWithCond = function(strRule) {
	this.strRule = strRule
}
StartWithCond.prototype.test = function(path) {
	return this.strRule == ("" + path).substring(0, this.strRule.length)
}

var buildCond = function(cond0) {
	return typeof cond0 == "string" ? new StartWithCond(cond0) : cond0
}

var Resolver = function() {
	this.rules = [] 
}

Resolver.prototype.resolve = function(path) {
	for (var i=0; i<this.rules.length; i++) {
		var r = this.rules[i]
		if (r.cond.test(path)) {
			return r.value
		}
	}
	return null
}

/**
 * Builds a new rule. 
 * 
 * If the condition is a regex the test method is used to match the path.
 * If the condition is a string the path must start with the condition to match
 * 
 * @param cond Condition to be evaluated
 * @param value Value to be returned if the condition is true
 */
Resolver.prototype.push = function(cond, value) {
	this.rules.push({cond:buildCond(cond), value:value})
}

$.resolver = function() {
	return new Resolver()
}